import 'package:flutter/material.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/business.dart';
import 'package:mall/ui/widgets/default_bg.dart';
import 'package:mall/ui/widgets/loading_dialog.dart';
import 'package:mall/ui/widgets/fm_icon.dart';
import 'package:mall/utils/ext/widget_ext.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LoginPage extends StatelessWidget {
  final GlobalKey<FormState> _loginKey = GlobalKey<FormState>();
  final TextEditingController _accountController = TextEditingController();
  final TextEditingController _passWordController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // _userViewModel = Provider.of<UserViewModel>(context);
    // _loginViewModel = LoginViewModel(_userViewModel);
    return APPScaffold(
      // title: AppStrings.LOGIN,
      actions: [
        TextButton(
            onPressed: () {
              // NavigatorUtil.goLoginPage(context);
            },
            child: Text(
              '账号密码登录',
              style: APPTextStyle.color_666666_size_13,
            )),
      ],
      body: SingleChildScrollView(
        child: Container(
          alignment: Alignment.centerLeft,
          margin: EdgeInsets.only(left: 33.w, right: 33.w, top: 50),
          child: Form(
              key: _loginKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '验证码登录/注册',
                    style: TextStyle(
                      color: AppColors.C_222222,
                      fontSize: 25,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 53),
                  //输入账号
                  TextFormField(
                    maxLines: 1,
                    maxLength: 11,
                    keyboardType: TextInputType.phone,
                    validator: _validatorAccount,
                    decoration: InputDecoration(
                      icon: Icon(
                        FMICon.ACCOUNT,
                        color: AppColors.C_FF5722,
                        size: ScreenUtil().setWidth(AppDimens.DIMENS_80),
                      ),
                      hintText: AppStrings.ACCOUNT_HINT,
                      hintStyle: FMTextStyle.color_999999_size_12,
                      labelStyle: FMTextStyle.color_222222_size_13,
                      labelText: AppStrings.ACCOUNT,
                    ),
                    controller: _accountController,
                  ),
                  SizedBox(height: 20),
                  TextFormField(
                    maxLines: 1,
                    maxLength: 12,
                    obscureText: true,
                    validator: _validatorPassWord,
                    decoration: InputDecoration(
                      icon: Icon(
                        FMICon.PASS_WORD,
                        color: AppColors.C_FF5722,
                        size: ScreenUtil().setWidth(AppDimens.DIMENS_80),
                      ),
                      hintText: AppStrings.PASSWORD_HINT,
                      hintStyle: FMTextStyle.color_999999_size_12,
                      labelStyle: FMTextStyle.color_222222_size_13,
                      labelText: AppStrings.PASSWORD,
                    ),
                    controller: _passWordController,
                  ),
                  Padding(padding: EdgeInsets.only(top: 27.5)),
                  Container(
                    alignment: Alignment.center,
                    width: double.infinity,
                    height: 45,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(AppDimens.DIMENS_30)),
                      color: AppColors.C_FF5722,
                    ),
                    child: Text(AppStrings.LOGIN, style: FMTextStyle.color_ffffff_size_13),
                  ).onInkwellTap(() => _login(context)),
                  Padding(padding: EdgeInsets.only(top: ScreenUtil().setHeight(AppDimens.DIMENS_13))),
                  Container(
                      alignment: Alignment.centerRight,
                      margin: EdgeInsets.only(right: 15),
                      child: InkWell(
                        onTap: () => _goRegister(context),
                        child: Text(
                          AppStrings.IMMEDIATELY_REGISTER,
                          style: FMTextStyle.color_222222_size_13,
                        ),
                      ))
                ],
              )),
        ),
      ),
    );
  }

  _goRegister(BuildContext context) {
    NavigatorUtil.goRegister(context);
  }

  void _login(BuildContext context) {
    FocusScope.of(context).unfocus();
    if (_loginKey.currentState!.validate()) {
      showLoginDialog(context);
      // _loginViewModel.login(_accountController.text, _passWordController.text).then((response) {
      //   Navigator.pop(context);
      //   if (!response) {
      //     HUDUtil.toast(_loginViewModel.errorMessage);
      //   } else {
      //     Provider.of<UserViewModel>(context, listen: false).refreshData();
      //     Provider.of<CartViewModel>(context, listen: false).queryCart();
      //     Navigator.pop(context);
      //   }
      // }, onError: (error) {
      //   HUDUtil.toast(_loginViewModel.errorMessage);
      // });
      Business.login(_accountController.text, _passWordController.text).then((response) {
        Navigator.pop(context);
      }).catchError((e) {
        Navigator.pop(context);
      });
    }
  }

  void showLoginDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return LoadingDialog(
            title: AppStrings.LOGINING,
            textColor: AppColors.C_999999,
            titleSize: AppDimens.DIMENS_13,
            indicatorRadius: 22.5,
          );
        });
  }

  String? _validatorAccount(String? value) {
    if (value != null && value.length < 11) {
      return AppStrings.ACCOUNT_RULE;
    }
    return null;
  }

  String? _validatorPassWord(String? value) {
    if (value != null && value.length < 6) {
      return AppStrings.PASSWORD_HINT;
    }
    return null;
  }
}
